package com.hly.musicbackground.model.index.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hly.musicbackground.Final.SelectConditionFinal;
import com.hly.musicbackground.Utils.RequestContextUtils;
import com.hly.musicbackground.dao.SelectConditionDao;
import com.hly.musicbackground.domain.SelectCondition;
import com.hly.musicbackground.domain.User;
import com.hly.musicbackground.model.boots.params.FictionQueryParams;
import com.hly.musicbackground.model.boots.service.FictionService;
import com.hly.musicbackground.model.index.params.SelectParams;
import com.hly.musicbackground.model.index.service.SelectConditionService;
import com.hly.musicbackground.model.music.params.SongQueryParams;
import com.hly.musicbackground.model.music.service.SongService;
import com.hly.musicbackground.result.Code;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SelectConditionServiceImpl extends ServiceImpl<SelectConditionDao, SelectCondition> implements SelectConditionService {


    private final SongService songService;
    private final FictionService fictionService;

    @Override
    public <T> T getQueryById(int id, String type, Class<T> tClass) {
        SelectCondition one = getQueryById(id, type);
        if (one == null)
            return null;
        return JSON.parseObject(one.getContent(), tClass);
    }

    @Override
    public SelectCondition getQueryById(int id, String type) {
        SelectCondition one = lambdaQuery().eq(SelectCondition::getLink, id)
                .eq(SelectCondition::getType, type).one();
        return one;
    }

    @Override
    public List getDataList(int id, String type) {
        List listData = null;
        if (type.startsWith("book")){
            FictionQueryParams query = getQueryById(id,
                    SelectConditionFinal.HOME_BOOK, FictionQueryParams.class);
            if (query != null)
                listData = fictionService.fictionQueryList(query).getList();
        }else if (type.startsWith("song")) {
            SongQueryParams query = getQueryById(id,
                    SelectConditionFinal.HOME_SONG, SongQueryParams.class);
            if (query != null)
                listData = songService.getQueryList(query).getList();
        }
        return listData;
    }

    @Override
    public void edit(SelectCondition selectCondition) {
        //User user = RequestContextUtils.getUser();
        //
        //if (user.getRoot() == 0){
        //    Code.BUSINESS_ERR("游客无法制定筛选条件");
        //}else if (user.getRoot() == 1){
        //    Code.BUSINESS_ERR("用户无法制定筛选条件");
        //}else if (!(SelectConditionFinal.HOME_BOOK.equals(selectCondition.getType()) ||
        //        SelectConditionFinal.HOME_SONG.equals(selectCondition.getType()))){
        //    Code.BUSINESS_ERR("类型不正确");
        //}

        if (selectCondition.getSelectId() == 0){
            save(selectCondition);
        }else {
            SelectCondition byId = getById(selectCondition.getSelectId());
            if (byId == null){
                Code.BUSINESS_ERR("不存在该条件");
            }
            byId.setLink(selectCondition.getLink());
            byId.setContent(selectCondition.getContent());
            byId.setType(selectCondition.getType());
            updateById(byId);
        }
    }

    @Override
    public List<SelectCondition> getSelectList(SelectParams params) {
        List<SelectCondition> list = lambdaQuery().eq(params.getLink() != 0, SelectCondition::getLink, params.getLink())
                .eq(params.getType() != null, SelectCondition::getType, params.getType())
                .list();
        return list;
    }

}
